package seteam12;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRadioButton;

public class SudokuView extends JFrame implements Observer {

    /**
     * version string to avoid warning
     */
    private static final long serialVersionUID = 1L;

    // private JTable sudokuTable;
    private SudokuTable sudokuTable;
    private SudokuBoard board;

    JRadioButton radioRandom;
    JRadioButton radioBacktracking;
    JRadioButton radioBruteforce;
    
    JRadioButton radioEasy;
    JRadioButton radioMedium;
    JRadioButton radioHard;

    JButton buttonSolve;
    JButton buttonReset;
    JButton buttonNewSudoku;

    ButtonGroup grp;
    ButtonGroup grpDifficulty;
    private SudokuController controller;

    public SudokuView(SudokuBoard board, SudokuController controller) {
	super("Sudoku");
	this.controller = controller;
	this.board = board;
	initView();
	// TODO: add layout, controls etc.
    }

    public void initView() {
	GridBagLayout layout = new GridBagLayout();
	getContentPane().setLayout(layout);
	GridBagConstraints c = new GridBagConstraints();
	c.fill = GridBagConstraints.HORIZONTAL;

	sudokuTable = new SudokuTable(board.getSize());
	sudokuTable.addTableModelListener(controller);

	c.gridx = 1;
	c.gridy = 0;
	c.gridwidth = 4;
	c.gridheight = 2;
	layout.setConstraints(sudokuTable, c);
	getContentPane().add(sudokuTable);

	c.gridx = 0;
	c.gridy = 0;
	c.gridwidth = 1;
	c.gridheight = 1;
	c.anchor = GridBagConstraints.NORTH;

	grp = new ButtonGroup();

	radioRandom = new JRadioButton("Random", true);
	grp.add(radioRandom);
	c.gridx = 1;
	c.gridy = 4;
	c.gridwidth = 1;
	layout.setConstraints(radioRandom, c);
	getContentPane().add(radioRandom);

	radioBacktracking = new JRadioButton("Backtracking", false);
	grp.add(radioBacktracking);
	c.gridx = 2;
	layout.setConstraints(radioBacktracking, c);
	getContentPane().add(radioBacktracking);

	radioBruteforce = new JRadioButton("Brute Force", false);
	grp.add(radioBruteforce);
	c.gridx = 3;
	layout.setConstraints(radioBruteforce, c);
	getContentPane().add(radioBruteforce);
	
	//ButtonGroup for the difficulty levels
	grpDifficulty = new ButtonGroup();
	
	radioEasy = new JRadioButton("Easy", true);
	grpDifficulty.add(radioEasy);
	c.gridx = 1;
	c.gridy = 3;
	layout.setConstraints(radioEasy, c);
	getContentPane().add(radioEasy);
	
	radioMedium = new JRadioButton("Medium", false);
	grpDifficulty.add(radioMedium);
	c.gridx = 2;
	layout.setConstraints(radioMedium, c);
	getContentPane().add(radioMedium);
	
	radioHard = new JRadioButton("Hard", false);
	grpDifficulty.add(radioHard);
	c.gridx = 3;
	layout.setConstraints(radioHard, c);
	getContentPane().add(radioHard);
	
	buttonSolve = new JButton("Solve!");
	c.gridx = 4;
	c.gridy = 4;
	layout.setConstraints(buttonSolve, c);
	getContentPane().add(buttonSolve);
	
	buttonNewSudoku = new JButton("New Sudoku!");
	c.gridx = 4;
	c.gridy = 3;
	layout.setConstraints(buttonNewSudoku, c);
	getContentPane().add(buttonNewSudoku);

	buttonReset = new JButton("Reset!");
	c.gridx = 4;
	c.gridy = 2;
	layout.setConstraints(buttonReset, c);
	getContentPane().add(buttonReset);

    }

    @Override
    public void update(Observable observed, Object event) {
	if (!(observed instanceof SudokuBoard && board == observed))
	    return;

	if (!(event instanceof SudokuBoardChangeEvent))
	    return;

	SudokuBoardChangeEvent changeEvent = (SudokuBoardChangeEvent) event;
	if (changeEvent.isCompleteRefreshRequired()) {
	    completeBoardRefresh();
	} else {
	    for (Cell cell : changeEvent.getChangedCells()) {
		sudokuTable.setValueAt(cell.value, cell.line, cell.column);
	    }
	}

    }

    private void completeBoardRefresh() {
	sudokuTable.reset();
	for (int row = 0; row < board.getSize(); row++) {
	    for (int col = 0; col < board.getSize(); col++) {
		sudokuTable.setValueAt(board.getValue(row, col), row, col);
		if (board.getFixedCells().contains(new Cell(row, col))) {
		    sudokuTable.setFixed(row, col);
		}
	    }
	}

    }
}
